package kr.co.mustgo.model.service.freeboard;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.co.mustgo.domain.freeboard.FreeBoard;
import kr.co.mustgo.model.dao.freeboard.CommentDAO;
import kr.co.mustgo.model.dao.freeboard.FreeBoardDAO;
import kr.co.mustgo.util.BoardUtilities;
import kr.co.mustgo.util.PagingBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FreeBoardServiceImpl implements FreeBoardService{

	@Autowired
	private FreeBoardDAO freeBoardDAO;
	
	@Autowired
	private CommentDAO commentDAO;
	
	@Override
	public void writeContent(FreeBoard freeBoard) {
		freeBoardDAO.insertContentForNewCount(freeBoard);
	}

	@Override
	public FreeBoard getContentByCode(int code) {
		freeBoardDAO.updateViewCount(code);
		//2. DAO의 메소드를 이용해 글 정보 조회
		FreeBoard free = freeBoardDAO.selectContentByCode(code);
		free.setComment(commentDAO.selectCommentList(code));
		return free;
	}

	@Override
	public Map getFreeBoardList(int page) {
		HashMap map = new HashMap();

		List<FreeBoard>list = freeBoardDAO.selectFreeBoardList(page);
		map.put("list", list);
		int totalContent = freeBoardDAO.selectTotalContent();
		PagingBean pagingBean = new PagingBean(totalContent, page);
		map.put("pagingBean", pagingBean);
		return map;
	}

	@Override
	public FreeBoard getContentByNoForForm(int code) {
		FreeBoard freeBoard = freeBoardDAO.selectContentByCode(code);
		
		return freeBoard;
	}

	@Override
	public void modifyContent(FreeBoard freeBoard) {
		freeBoardDAO.updateContent(freeBoard);
	}

	@Override
	public void deleteContentByCode(int code) {
		freeBoardDAO.deleteContentByCode(code);
	}

}
